package com.aixin.project.listener;

import com.aixin.project.data.AxSession;
import com.aixin.project.data.CommonConstants;
import com.aixin.project.util.AxMemcached;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 12-5-16
 * Time: 下午6:39
 * To change this template use File | Settings | File Templates. http://zhidao.baidu.com/question/63523391.html
 */
public class AxOnlineListener implements HttpSessionListener, ServletContextListener, HttpSessionAttributeListener {
    ServletContext application = null;

    HttpSession session = null;

    int count, count1, count2 = 0;

    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        System.out.println("新的会话: sessionid:"+httpSessionEvent.getSession().getId());
        count = (Integer) application.getAttribute("anonyUserCount");
        count++;
        application.setAttribute("anonyUserCount", count);
    }

    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        session = httpSessionEvent.getSession();
        if (session.getAttribute(CommonConstants.USER_SESSION) != null) {
            System.out.println("用户注销....");
            AxSession axSession = (AxSession) session.getAttribute(CommonConstants.USER_SESSION);
            if (axSession.isUser()) {
                HashMap<Long, Integer> usermap = (HashMap<Long, Integer>) application.getAttribute("registerUser");
                Integer logincount = usermap.get(axSession.getUid());
                if (logincount != null && logincount > 0) {
                    logincount--;
                    usermap.put(axSession.getUid(), logincount);
                    application.setAttribute("registerUser", usermap);
                }
                List<String> joinlistKey = (List<String>) AxMemcached.get(CommonConstants.MKEY_LIVEBROAD_JOINLIST + axSession.getUid());
                if(joinlistKey!=null){
                    for(String joinKey : joinlistKey){
                        List<Long> joinList = (List<Long>) AxMemcached.get(joinKey);
                        if(joinList!=null && joinList.size()>0){
                            joinList.remove(new Long(axSession.getGenseeuid()));
                        }
                        AxMemcached.set(joinKey,joinList,new Date(CommonConstants.MEMCACHED_SESSION_TIMEOUT_HOUR * 5 * 1000));
                    }
                }
                AxMemcached.delete(CommonConstants.MKEY_LIVEBROAD_JOINLIST + axSession.getUid());
            } else if (axSession.isExpert()) {
                HashMap<Long, Integer> usermap = (HashMap<Long, Integer>) application.getAttribute("registerExpert");
                Integer logincount = usermap.get(axSession.getUid());
                if (logincount != null && logincount > 0) {
                    logincount--;
                    usermap.put(axSession.getUid(), logincount);
                    application.setAttribute("registerExpert", usermap);
                }
            }
        } else {
            System.out.println("用户超时....");
            count = (Integer) application.getAttribute("anonyUserCount");
            if (count > 0) {
                count--;
            }
            application.setAttribute("anonyUserCount", count);
        }
    }

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        // tomcat启动时创建用于保存人数的application对象
        application = servletContextEvent.getServletContext();
        application.setAttribute("registerUser", new HashMap<Long, Integer>());// 已注册学校
        application.setAttribute("registerExpert", new HashMap<Long, Integer>());// 已注册专家
        application.setAttribute("anonyUserCount", new Integer(0));// 匿名用户数
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("session added attribute name:"+httpSessionBindingEvent.getName());
        if (!CommonConstants.USER_SESSION.equals(httpSessionBindingEvent.getName())) {

            return;
        }
        session = httpSessionBindingEvent.getSession();
        if (session.getAttribute(CommonConstants.USER_SESSION) != null) {
            session = httpSessionBindingEvent.getSession();
            AxSession axSession = (AxSession) session.getAttribute(CommonConstants.USER_SESSION);
            if (axSession.isUser()) {
                HashMap<Long, Integer> usermap = (HashMap<Long, Integer>) application.getAttribute("registerUser");
                Integer usercount = usermap.get(axSession.getUid());
                usercount = (usercount == null ? 0 : usercount);
                usercount++;
                usermap.put(axSession.getUid(), usercount);
                application.setAttribute("registerUser", usermap);
            } else if (axSession.isExpert()) {
                HashMap<Long, Integer> usermap = (HashMap<Long, Integer>) application.getAttribute("registerExpert");
                Integer usercount = usermap.get(axSession.getUid());
                usercount = (usercount == null ? 0 : usercount);
                usercount++;
                usermap.put(axSession.getUid(), usercount);
                application.setAttribute("registerExpert", usermap);
            }

            count = (Integer) application.getAttribute("anonyUserCount");
            if (count > 0) {
                count--;
            }
            application.setAttribute("anonyUserCount", count);
        }
    }

    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        if (!CommonConstants.USER_SESSION.equals(httpSessionBindingEvent.getName())) {
            return;
        }
        if (session.getAttribute(CommonConstants.USER_SESSION) != null) {
            session = httpSessionBindingEvent.getSession();
            AxSession axSession = (AxSession) session.getAttribute(CommonConstants.USER_SESSION);
            if (axSession.isUser()) {
                HashMap<Long, Integer> usermap = (HashMap<Long, Integer>) application.getAttribute("registerUser");
                Integer usercount = usermap.get(axSession.getUid());
                usercount = (usercount == null ? 0 : usercount);
                if(usercount>=1)
                    usercount--;
                usermap.put(axSession.getUid(), usercount);
                application.setAttribute("registerUser", usermap);
            } else if (axSession.isExpert()) {
                HashMap<Long, Integer> usermap = (HashMap<Long, Integer>) application.getAttribute("registerExpert");
                Integer usercount = usermap.get(axSession.getUid());
                usercount = (usercount == null ? 0 : usercount);
                if(usercount>=1)
                    usercount--;
                usermap.put(axSession.getUid(), usercount);
                application.setAttribute("registerExpert", usermap);
            }

            count = (Integer) application.getAttribute("anonyUserCount");
            if (count >= 0) {
                count++;
            }
            application.setAttribute("anonyUserCount", count);
        }
    }

    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
